//
//  HomePageListPicView.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//

#define ViewPadding 16.0
#define ItemMargin 4.0
#define ItemWidth (kScreenW - ItemMargin * 2 - ViewPadding * 2)/3

#define ItemOnlyHeight ItemWidth * 2 + ItemMargin

#import "HomePageListPicView.h"

@interface HomePageListPicView ()
@property(nonatomic, strong) UIView *singalView; // 只有一张图
@property(nonatomic, strong) UIView *doubleView; // 2张 or 4张图
@property(nonatomic, strong) UIView *normalTypeView; // 其他
@end

@implementation HomePageListPicView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

                    
- (void)setUI{
    
    
    
    
    [self addSubview:self.singalView];
    [self.singalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(kScreenW);
        make.height.equalTo(@0);
    }];
    
    [self addSubview:self.doubleView];
    [self.doubleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(kScreenW);
        make.height.equalTo(@0);
    }];
    
    [self addSubview:self.normalTypeView];
    [self.normalTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(kScreenW);
        make.height.equalTo(@0);
    }];
}

- (void)setIndex:(NSNumber *)index urls:(NSArray *)urls{
    if (index.intValue == 1) {
        
        self.singalView.hidden = NO;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = YES;
        
        [self.singalView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.width.mas_equalTo(kScreenW);
            make.height.equalTo(@(ItemOnlyHeight));
        }];
        [self.singalView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.singalView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

            }];
        }];
        
    } else if (index.intValue == 2 ||  index.intValue == 4) {
        
        self.singalView.hidden = YES;
        self.doubleView.hidden = NO;
        self.normalTypeView.hidden = YES;
        
        [self.doubleView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.doubleView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.hidden = YES;
            if (idx < index.intValue) {
                obj.hidden = NO;
                [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    
                }];
            }
        }];
        
        NSInteger row = index.intValue / 2;
        CGFloat h = row*ItemWidth + (row - 1) * ItemMargin;
        
        [self.doubleView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.width.mas_equalTo(kScreenW);
            make.height.equalTo(@(h));
        }];
    
    } else {
        
        self.singalView.hidden = YES;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = NO;
        
        [self.normalTypeView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.normalTypeView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.hidden = YES;
            if (idx < index.intValue) {
                obj.hidden = NO;
                [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]] placeholderImage:[UIImage imageNamed:@""]];
            }
        }];

        NSInteger row = index.intValue % 3 == 0 ? index.intValue / 3 : (index.intValue / 3 + 1);
        CGFloat h = index == 0?0:row * ItemWidth + (row - 1) * ItemMargin;
        [self.normalTypeView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.width.mas_equalTo(kScreenW);
            make.height.equalTo(@(h));
        }];
    }
}

- (UIView *)singalView{
    
    if (!_singalView) {
        _singalView = [[UIView alloc] init];
        _singalView.hidden = YES;
        
        UIImageView *img = [[UIImageView alloc] init];
        img.backgroundColor = RGBA(240, 240, 240, 1);
        img.userInteractionEnabled = YES;
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.layer.masksToBounds = YES;
        img.tag = 0;
        UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showDetaiWithImgViewAtIndex:)];
        [img addGestureRecognizer:tag];
        [_singalView addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.singalView).offset(ViewPadding);
            make.top.equalTo(self.singalView);
            make.size.mas_equalTo(CGSizeMake(ItemOnlyHeight, ItemOnlyHeight));
        }];
    }
    return _singalView;
}

- (UIView *)doubleView{
    
    if (!_doubleView) {
        _doubleView = [[UIView alloc] init];
        _doubleView.hidden = YES;
        
        UIImageView *lastImg = nil;
        CGFloat picW = ItemWidth;
        for (NSInteger i = 0; i < 4; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.userInteractionEnabled = YES;
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.masksToBounds = YES;
            img.tag = i;
            UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showDetaiWithImgViewAtIndex:)];
            [img addGestureRecognizer:tag];
            [_doubleView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                if (lastImg) {
                    if (i % 2 == 0) {
                        make.left.equalTo(self.doubleView).offset(ViewPadding);
                        make.top.equalTo(lastImg.mas_bottom).offset(ItemMargin);
                    } else {
                        make.left.equalTo(lastImg.mas_right).offset(ItemMargin);
                        make.top.equalTo(lastImg);
                    }
                } else {
                    make.left.equalTo(self.doubleView).offset(ViewPadding);
                    make.top.equalTo(self.doubleView);
                }
                make.size.mas_equalTo(CGSizeMake(picW, picW));
            }];
            lastImg = img;
        }
    }
    return _doubleView;
}

- (UIView *)normalTypeView{
    
    if (!_normalTypeView) {
        _normalTypeView = [[UIView alloc] init];
        _normalTypeView.hidden = YES;
        
        UIImageView *lastImg = nil;
        for (NSInteger i = 0; i < 9; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.userInteractionEnabled = YES;
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.masksToBounds = YES;
            img.tag = i;
            UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showDetaiWithImgViewAtIndex:)];
            [img addGestureRecognizer:tag];
            [_normalTypeView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                if (lastImg) {
                    if (i % 3 == 0) {
                        make.left.equalTo(self.normalTypeView).offset(ViewPadding);
                        make.top.equalTo(lastImg.mas_bottom).offset(ItemMargin);
                    } else {
                        make.left.equalTo(lastImg.mas_right).offset(ItemMargin);
                        make.top.equalTo(lastImg);
                    }
                } else {
                    make.left.equalTo(self.normalTypeView).offset(ViewPadding);
                    make.top.equalTo(self.normalTypeView);
                }
                make.size.mas_equalTo(CGSizeMake(ItemWidth, ItemWidth));
            }];
            lastImg = img;
        }
    }
    return _normalTypeView;
}

- (void)showDetaiWithImgViewAtIndex:(UIGestureRecognizer *)gestureRecognizer{
    
    UIView *imgV = gestureRecognizer.view;
    
    if (self.didImageAtIndex) {
        self.didImageAtIndex(imgV.tag);
    }
}

@end
